home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 401-425 / disk_407 / flex / src.lzh / src / yylex.c < prev   
C/C++ Source or Header  |  1990-06-27  |  4KB  |  217 lines

  1. /* yylex - scanner front-end for flex */
  2.  
  3. /*-
  4.  * Copyright (c) 1990 The Regents of the University of California.
  5.  * All rights reserved.
  6.  *
  7.  * This code is derived from software contributed to Berkeley by
  8.  * Vern Paxson.
  9.  * 
  10.  * The United States Government has rights in this work pursuant
  11.  * to contract no. DE-AC03-76SF00098 between the United States
  12.  * Department of Energy and the University of California.
  13.  *
  14.  * Redistribution and use in source and binary forms are permitted provided
  15.  * that: (1) source distributions retain this entire copyright notice and
  16.  * comment, and (2) distributions including binaries display the following
  17.  * acknowledgement:  ``This product includes software developed by the
  18.  * University of California, Berkeley and its contributors'' in the
  19.  * documentation or other materials provided with the distribution and in
  20.  * all advertising materials mentioning features or use of this software.
  21.  * Neither the name of the University nor the names of its contributors may
  22.  * be used to endorse or promote products derived from this software without
  23.  * specific prior written permission.
  24.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
  25.  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
  26.  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  27.  */
  28.  
  29. #ifndef lint
  30. static char rcsid[] =
  31.     "@(#) $Header: /usr/fsys/odin/a/vern/flex/RCS/yylex.c,v 2.5 90/06/27 23:48:40 vern Exp $ (LBL)";
  32. #endif
  33.  
  34. #include <ctype.h>
  35. #include "flexdef.h"
  36. #include "parse.h"
  37.  
  38.  
  39. /* ANSI C does not guarantee that isascii() is defined */
  40. #ifndef isascii
  41. #define isascii(c) ((c) <= 0177)
  42. #endif
  43.  
  44.  
  45. /* yylex - scan for a regular expression token
  46.  *
  47.  * synopsis
  48.  *
  49.  *   token = yylex();
  50.  *
  51.  *     token - return token found
  52.  */
  53.  
  54. int yylex()
  55.  
  56.     {
  57.     int toktype;
  58.     static int beglin = false;
  59.  
  60.     if ( eofseen )
  61.     toktype = EOF;
  62.     else
  63.     toktype = flexscan();
  64.  
  65.     if ( toktype == EOF || toktype == 0 )
  66.     {
  67.     eofseen = 1;
  68.  
  69.     if ( sectnum == 1 )
  70.         {
  71.         synerr( "premature EOF" );
  72.         sectnum = 2;
  73.         toktype = SECTEND;
  74.         }
  75.  
  76.     else if ( sectnum == 2 )
  77.         {
  78.         sectnum = 3;
  79.         toktype = 0;
  80.         }
  81.  
  82.     else
  83.         toktype = 0;
  84.     }
  85.  
  86.     if ( trace )
  87.     {
  88.     if ( beglin )
  89.         {
  90.         fprintf( stderr, "%d\t", num_rules + 1 );
  91.         beglin = 0;
  92.         }
  93.  
  94.     switch ( toktype )
  95.         {
  96.         case '<':
  97.         case '>':
  98.         case '^':
  99.         case '$':
  100.         case '"':
  101.         case '[':
  102.         case ']':
  103.         case '{':
  104.         case '}':
  105.         case '|':
  106.         case '(':
  107.         case ')':
  108.         case '-':
  109.         case '/':
  110.         case '\\':
  111.         case '?':
  112.         case '.':
  113.         case '*':
  114.         case '+':
  115.         case ',':
  116.         (void) putc( toktype, stderr );
  117.         break;
  118.  
  119.         case '\n':
  120.         (void) putc( '\n', stderr );
  121.  
  122.         if ( sectnum == 2 )
  123.             beglin = 1;
  124.  
  125.         break;
  126.  
  127.         case SCDECL:
  128.         fputs( "%s", stderr );
  129.         break;
  130.  
  131.         case XSCDECL:
  132.         fputs( "%x", stderr );
  133.         break;
  134.  
  135.         case WHITESPACE:
  136.         (void) putc( ' ', stderr );
  137.         break;
  138.  
  139.         case SECTEND:
  140.         fputs( "%%\n", stderr );
  141.  
  142.         /* we set beglin to be true so we'll start
  143.          * writing out numbers as we echo rules.  flexscan() has
  144.          * already assigned sectnum
  145.          */
  146.  
  147.         if ( sectnum == 2 )
  148.             beglin = 1;
  149.  
  150.         break;
  151.  
  152.         case NAME:
  153.         fprintf( stderr, "'%s'", nmstr );
  154.         break;
  155.  
  156.         case CHAR:
  157.         switch ( yylval )
  158.             {
  159.             case '<':
  160.             case '>':
  161.             case '^':
  162.             case '$':
  163.             case '"':
  164.             case '[':
  165.             case ']':
  166.             case '{':
  167.             case '}':
  168.             case '|':
  169.             case '(':
  170.             case ')':
  171.             case '-':
  172.             case '/':
  173.             case '\\':
  174.             case '?':
  175.             case '.':
  176.             case '*':
  177.             case '+':
  178.             case ',':
  179.             fprintf( stderr, "\\%c", yylval );
  180.             break;
  181.  
  182.             default:
  183.             if ( ! isascii( yylval ) || ! isprint( yylval ) )
  184.                 fprintf( stderr, "\\%.3o", yylval );
  185.             else
  186.                 (void) putc( yylval, stderr );
  187.             break;
  188.             }
  189.             
  190.         break;
  191.  
  192.         case NUMBER:
  193.         fprintf( stderr, "%d", yylval );
  194.         break;
  195.  
  196.         case PREVCCL:
  197.         fprintf( stderr, "[%d]", yylval );
  198.         break;
  199.  
  200.         case EOF_OP:
  201.         fprintf( stderr, "<<EOF>>" );
  202.         break;
  203.  
  204.         case 0:
  205.         fprintf( stderr, "End Marker" );
  206.         break;
  207.  
  208.         default:
  209.         fprintf( stderr, "*Something Weird* - tok: %d val: %d\n",
  210.              toktype, yylval );
  211.         break;
  212.         }
  213.     }
  214.         
  215.     return ( toktype );
  216.     }
  217.